package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.heima.file.MinIOTemplate;
import com.github.pagehelper.PageHelper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.util.WmThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 10:59:16
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private WmMaterialDao wmMaterialDao;

    @Autowired
    private MinIOTemplate minIOTemplate;



    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer  id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }

    @Override
    public ResponseResult uploadPic(MultipartFile multipartFile) throws IOException {
        //1. 校验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2. 上传图片到minio
        String originalFilename = multipartFile.getOriginalFilename();
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName = fileName + suffix;

        String url = minIOTemplate.uploadImgFile("", fileName, multipartFile.getInputStream());

        //3. 保存素材数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId((long)WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());

        wmMaterialDao.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1. 校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        //2. 执行分页条件查询
        //2.1 封装分页参数
        WmMaterial queryParam = new WmMaterial();
        queryParam.setUserId((long)WmThreadLocalUtil.getUser().getId());
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            queryParam.setIsCollection(String.valueOf(dto.getIsCollection()));
        }
        //2.2 开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmMaterial> page = wmMaterialDao.queryAll(queryParam);

        //3. 封装数据返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getResult());

        return result;
    }


    /**
     * 通过ID查询单条数据(素材收藏)
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Integer id) {
        WmMaterial wmMaterial = this.wmMaterialDao.queryById(id);
        wmMaterial.setIsCollection(1);
        return wmMaterial;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Object cancelQueryById(Integer id) {
        WmMaterial wmMaterial = this.wmMaterialDao.queryById(id);
        wmMaterial.setIsCollection(0);
        return wmMaterial;
    }

}
